package com.example.ggpicturegg.myAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.ggpicturegg.R;
import com.example.ggpicturegg.bottom.Talk_M_Activity;
import com.example.ggpicturegg.bottom.Talk_b;
import com.example.ggpicturegg.first.Like_list;
import com.example.ggpicturegg.first.RecyclerviewActivity;
import com.example.ggpicturegg.tool.User;


import java.util.ArrayList;
import java.util.List;

public class SortAdapter  extends BaseAdapter {
    private List<User> list=null;
    private Context mContext;
    private String sname;
    private String rname;

    public SortAdapter(Context mContext, List<User> list,String name) {
        this.list=list;
        this.mContext=mContext;
        this.sname = name;
    }

    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView( final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder;
       // System.out.println("------------ " + position+rname);
        final User user = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.catalog = (TextView) view.findViewById(R.id.catalog);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取首字母作为目录catalog
        String catalog = list.get(position).getFirstLetter();

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(catalog)){
            viewHolder.catalog.setVisibility(View.VISIBLE);
            viewHolder.catalog.setText(user.getFirstLetter().toUpperCase());
        }else{
            viewHolder.catalog.setVisibility(View.GONE);
        }

        viewHolder.name.setText(position+"-"+this.list.get(position).getName());


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Yes  =========--====   "+position);
                System.out.println("sname  =============   "+sname);
                System.out.println("rname  =============   "+list.get(position).getName());
                Intent intent = new Intent(mContext, Talk_M_Activity.class);
                Log.d("--Re 发送到 like 的值-- ", "--  is         " + list.get(position).getName() + " "+ sname );
                Bundle bundle = new Bundle();
                //bundle.putInt("max_id",ans);
                bundle.putString("Send_name",sname);
                bundle.putString("Rend_name",list.get(position).getName());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        return view;
    }


    final static class ViewHolder {
        TextView catalog;
        TextView name;
    }



    /**
     * 获取catalog首次出现位置
     */
    public int getPositionForSection(String catalog) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getFirstLetter();
            if (catalog.equalsIgnoreCase(sortStr)) {
                return i;
            }
        }
        return -1;
    }
}